package siwi.map.android;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.app.SherlockDialogFragment;
import com.wmw.Animation.EnlargeAnimationOut;
import com.wmw.Animation.ShrinkAnimationOut;
import com.wmw.Animation.ViewAction;
import com.wmw.Utility.Common;
import com.wmw.Utility.MSG;
import com.wmw.Utility.RegularExpression;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnticipateInterpolator;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class RegisterThirdStep extends SherlockActivity implements
		ActionBar.TabListener, OnClickListener {
	// 切到桌面然后返回软件,注册第三步tab变多了
	// 同意用户协议
	private View _questionLayout, _emailLayout;
	private TextView _question;
	private RegisterType _type;

	enum RegisterType {
		EMAIL, QUESTION
	}

	private InputBox _answer, _email, _qq;

	private EditText view_userName;
	private EditText view_password;
	private EditText view_passwordConfirm;
	private Button view_submit;
	private Button view_clearAll;
	private static final int MENU_EXIT = Menu.FIRST - 1;
	private static final int MENU_ABOUT = Menu.FIRST;

	private boolean areMenusShowing;

	private Intent _gpsService;
	private GPSAddrReceiverForRSS _receiver;

	private boolean _inited;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// setTheme(R.style.Theme_Sherlock_Light);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.register_third_step);

//		// Custom action bar.
//		ActionBar actionBar = getSupportActionBar();
//		ActionBar.LayoutParams params = new ActionBar.LayoutParams(
//				LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,
//				Gravity.CENTER);
//		View view = LayoutInflater.from(this).inflate(
//				R.layout.order_dish_detail_action_bar, null);
//		actionBar.setCustomView(view, params);
//		actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
//		actionBar.setDisplayShowCustomEnabled(true);

		
		_questionLayout = findViewById(R.id.RTS_QuestionLayout);
		_emailLayout = findViewById(R.id.RTS_EmailLayout);

		findViewById(R.id.RTS_QuestionClickListener).setOnClickListener(
				new OnClickListener() {
					@Override
					public void onClick(View arg0) {
						showDialog(0);
					}
				});

		EditText etAnswer = (EditText) findViewById(R.id.RTS_QuestionAnswer);
		ImageView ivClearAnswer = (ImageView) findViewById(R.id.RTS_ClearQuestionAnswer);
		_answer = new InputBox(this, etAnswer, ivClearAnswer, null,
				InputBox._DefaultPadding, RegularExpression.Normal, new InputBox.NoAnyAction());

		EditText etEmail = (EditText) findViewById(R.id.RTS_Email);
		ImageView ivClearEmail = (ImageView) findViewById(R.id.RTS_ClearEmail);
		_email = new InputBox(this, etEmail, ivClearEmail, null,
				InputBox._DefaultPadding, RegularExpression.Email_OR_QQ, new InputBox.NoAnyAction());

		// EditText etRestaurantName = (EditText)
		// findViewById(R.id.RSS_RestaurantName);
		// ImageView clearRestaurantName = (ImageView)
		// findViewById(R.id.RSS_ClearRestaurantName);
		// _restaurantName = new InputBox(this, etRestaurantName,
		// clearRestaurantName, null, etRestaurantName.getPaddingRight(),
		// RegularExpression.RestaurantName, null);
		//
		// EditText etPhoneNumber = (EditText)
		// findViewById(R.id.RSS_PhoneNumber);
		// ImageView clearPhoneNumber = (ImageView)
		// findViewById(R.id.RSS_ClearPhoneNumber);
		// _phoneNumber = new InputBox(this, etPhoneNumber, clearPhoneNumber,
		// null, etPhoneNumber.getPaddingRight(),
		// RegularExpression.PhoneNumber, null);
		//
		// EditText etAddressMoreDetail = (EditText)
		// findViewById(R.id.RSS_Address2);
		// ImageView clearAddressMoreDetail = (ImageView)
		// findViewById(R.id.RSS_ClearAddress2);
		// _address2 = new InputBox(this, etAddressMoreDetail,
		// clearAddressMoreDetail, null,
		// etAddressMoreDetail.getPaddingRight(),
		// RegularExpression.Address, null);
		//
		Button button = (Button) findViewById(R.id.RTS_CompleteRegister);
		button.setOnClickListener(this);
	}

	@Override
	public void onStart(){
		super.onStart();

		// Ensure to initialize once.
		if (!_inited){
			ActionBar actionBar = getSupportActionBar();
			actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
			ActionBar.Tab newTab = actionBar.newTab();
			newTab.setTabListener(RegisterThirdStep.this);
			newTab.setText("邮箱/QQ");
			actionBar.addTab(newTab);

			ActionBar.Tab newTab2 = actionBar.newTab();
			newTab2.setTabListener(RegisterThirdStep.this);
			newTab2.setText("问题");
			actionBar.addTab(newTab2);
			_inited = true;
		}
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		return new AlertDialog.Builder(RegisterThirdStep.this)
				.setTitle("")
				.setItems(R.array.select_dialog_items,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int which) {
								String[] items = getResources().getStringArray(
										R.array.select_dialog_items);
								Log.d(Common.GetLineInfo(), "You selected: "
										+ which + " , " + items[which]);
								_question = (TextView) findViewById(R.id.RTS_Question);
								_question.setText(items[which]);
							}
						}).create();
	}

	@Override
	public void onTabSelected(Tab tab, FragmentTransaction ft) {
		// TODO Auto-generated method stub

		RegisterType pos = RegisterType.values()[tab.getPosition()];
		_type = pos;
		Log.d(Common.GetLineInfo(), "Tab selected position -" + pos);
		switch (pos) {
		case EMAIL:
			_questionLayout.setVisibility(View.INVISIBLE);
			_emailLayout.setVisibility(View.VISIBLE);
			break;

		case QUESTION:
			_questionLayout.setVisibility(View.VISIBLE);
			_emailLayout.setVisibility(View.INVISIBLE);
			break;
		}
	}

	@Override
	public void onTabUnselected(Tab tab, FragmentTransaction ft) {

	}

	@Override
	public void onTabReselected(Tab tab, FragmentTransaction ft) {
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		ViewAction.startSpringMenuAnimations(v);

		switch (_type) {
		case EMAIL:
			if (!_email.isValide()) {
				Toast.makeText(RegisterThirdStep.this, "请输入正确的邮箱/QQ",
						Toast.LENGTH_SHORT).show();
				return;
			}
			break;

		case QUESTION:

			if (!_answer.isValide()) {
				Toast.makeText(RegisterThirdStep.this, "请输入正确的答案",
						Toast.LENGTH_SHORT).show();
				return;
			}
			break;
		}

		// TODO
		Intent intent = new Intent();
		intent.setClass(RegisterThirdStep.this, Login.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
		startActivity(intent);
	}

	// findViews();
	// setListener();
	// 然后执行注册的事情,访问远程服务器注册用户
}
//
// /** 1.初始化注册view组件 */
// private void findViews() {
// view_userName = (EditText) findViewById(R.id.registerUserName);
// view_password = (EditText) findViewById(R.id.registerPassword);
// view_passwordConfirm = (EditText) findViewById(R.id.registerPasswordConfirm);
// view_submit = (Button) findViewById(R.id.registerSubmit);
// view_clearAll = (Button) findViewById(R.id.registerClear);
// }
//
// private void setListener() {
// view_submit.setOnClickListener(submitListener);
// view_clearAll.setOnClickListener(clearListener);
// }
//
// /** 监听注册确定按钮 */
// private OnClickListener submitListener = new OnClickListener() {
// @Override
// public void onClick(View v) {
// String userName = view_userName.getText().toString();
// String password = view_password.getText().toString();
// String passwordConfirm = view_passwordConfirm.getText().toString();
// validateForm(userName, password, passwordConfirm);
// }
// };
//
// /** 清空监听按钮 */
// private OnClickListener clearListener = new OnClickListener() {
// @Override
// public void onClick(View v) {
// clearForm();
// }
// };
//
// /** 检查注册表单 */
// private void validateForm(String userName, String password, String password2)
// {
// StringBuilder suggest = new StringBuilder();
// Log.d(this.toString(), "validate");
// if (userName.length() < 1) {
// suggest.append(getText(R.string.suggust_userName) + "\n");
// }
// if (password.length() < 1 || password2.length() < 1) {
// suggest.append(getText(R.string.suggust_passwordNotEmpty) + "\n");
// }
// if (!password.equals(password2)) {
// suggest.append(getText(R.string.suggest_passwordNotSame));
// }
// if (suggest.length() > 0) {
// //sub是因为要出去最后一个 \n换行符
// Toast.makeText(this, suggest.subSequence(0, suggest.length() - 1),
// Toast.LENGTH_SHORT).show();
// }
// }
//
// /** 清空表单 */
// private void clearForm() {
// view_userName.setText("");
// view_password.setText("");
// view_passwordConfirm.setText("");
//
// view_userName.requestFocus();
// }
//
// @Override
// public boolean onCreateOptionsMenu(Menu menu) {
// super.onCreateOptionsMenu(menu);
// menu.add(0, MENU_EXIT, 0, R.string.MENU_EXIT);
// menu.add(0, MENU_ABOUT, 0, R.string.MENU_ABOUT);
// return true;
// }
//
// @Override
// public boolean onMenuItemSelected(int featureId, MenuItem item) {
// super.onMenuItemSelected(featureId, item);
// switch (item.getItemId()) {
// case MENU_EXIT:
// finish();
// break;
// case MENU_ABOUT:
// alertAbout();
// break;
// }
// ;
// return true;
// }
//
// /** 弹出关于对话框 */
// private void alertAbout() {
// new AlertDialog.Builder(Register.this).setTitle(R.string.MENU_ABOUT)
// .setMessage(R.string.aboutInfo).setPositiveButton(
// R.string.ok_label,
// new DialogInterface.OnClickListener() {
// public void onClick(
// DialogInterface dialoginterface, int i) {
// }
// }).show();
// }
// }
